<%@ page import="java.io.*, java.text.*, java.util.Date, java.sql.*, javax.servlet.*, javax.servlet.http.*, java.util.*" %>
<%!
public void check(String [] arr)
{
	for(int i=0;i<arr.length;i++)
	{
		while(arr[i] != null && arr[i].indexOf("\n") != -1)
		{
			arr[i] = arr[i].substring(0,arr[i].indexOf("\n")-1) + "<br/>" + arr[i].substring(arr[i].indexOf("\n")+1,arr[i].length());
		}
	}
}
%>
<%
String usernameS = (String) session.getAttribute("session_name");
String typeS = (String) session.getAttribute("type");
String idS = (String) session.getAttribute("id");
	
String driver ="com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/projects";
String username = "ia";
String password = "123";
String [] id = null;
String [] title = null;
String [] desc = null;
String [] ndate = null;


int size = 0;

try
{
	Class.forName(driver).newInstance();
	
	Connection connection = DriverManager.getConnection(url, username, password);
	
	Statement statement = connection.createStatement();
	Statement statement1 = connection.createStatement();
	Statement statementS1 = connection.createStatement();
	Statement statementS2 = connection.createStatement();
	
	String query = "select count(*) from news";
	String query1 = "select * from news";
	
	ResultSet rs = statement.executeQuery(query);
	if(rs.next())
	{
		size = rs.getInt(1);
	}
	
	ResultSet rs1 = statement.executeQuery(query1);
	id = new String[size];
	title = new String[size];
	desc = new String[size];
	ndate = new String[size];

		
	for(int i=0 ;rs1.next();i++)
	{
			id[i] = rs1.getString("id");
			title[i] = rs1.getString("Title");
			desc[i] = rs1.getString("description");
			ndate[i] = rs1.getString("Creation_Date");
	}
	
	check(desc);
	
	connection.close();
		
}catch(ClassNotFoundException cnfe)
{
		out.println("<br>Couldn't find class file" + cnfe); 
}catch(SQLException sqle)
{
		out.println("<br>SQL Exception: " + sqle);
}catch(InstantiationException ie)
{
		out.println("<br>Instant exception : " + ie);
}catch(IllegalAccessException iae)
{
		out.println("<br>Illegal access exception : " + iae);
}
out.println("<script type='text/javascript'>");
out.println("function init2() {");
for(int i = 0;i<size;i++) {
	out.println("description["+i+"] = new Desc();");
	out.println("description["+i+"].id = '"+ (id[i] != null ? id[i] : "") +"';");
	out.println("description["+i+"].t = '"+ (title[i] != null ? title[i] : "") +"';");
	out.println("description["+i+"].desc = '"+(desc[i] != null ? desc[i] : "")+"';");
	out.println("description["+i+"].ndate = '"+(ndate[i] != null ? ndate[i] : "")+"';");
	
}
out.println("}");
out.println("var usernameS='"+usernameS+"'");
out.println("</script>");

%>
<script type="text/javascript">
function news()
{
		var div = document.getElementById("nn");
		var out = "";
		out = "<table style='width:100%' border=0>";
		for(var i = description.length-1 ; i > description.length - 5; i--)
		{
				out += "<tr><td>"+description[i].ndate+"</td></td></tr><tr><td colspan=4><b>"+description[i].t+"</b><br><br></td></tr>";
		}
		out += "</table>";
	div.innerHTML = out;
}
</script>